package de.koller.million.render.util;

import de.koller.oceanFramework.tree.Sea3X;
import java.util.HashMap;
import java.util.Map;

public class Buffer<T> extends Sea3X {
	
	private final Map<String, T> content = new HashMap<>();
	
	public void addContent( String key, T value ) {
		if( content.containsKey( key ) )
			throw new ContainsKeyException( "Already contains key: " + key );
		content.put( key, value );
	}

	public T getContent( String key, IGenerate<T> generator ) {
		try {
			return getContent( key );
		} catch( ContainsKeyException ex ) {
			addContent( key, generator.create( key ) );
			return getContent( key );
		}
	}
	
	public T getContent( String key ) {
		if( !content.containsKey( key ) )
			throw new ContainsKeyException( key + " is not yet added" );
		return content.get( key );
	}
	
	public T removeContent( String key ) {
		return content.remove( key );
	}
	
	public static interface IGenerate<T> {
		public T create( String key );
	}

	public static class ContainsKeyException extends RuntimeException {
		public ContainsKeyException( String text ) {
			super( text );
		}
	}

}
